import { createRouter, createWebHistory } from 'vue-router'
import VueCookies from 'vue-cookies'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import("@/views/Home.vue")
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import("@/views/Login.vue")
    },
    {
      path: '/qqlogincallback',
      name: 'qq登录回调',
      component: () => import("@/views/QqLoginCallback.vue")
    },
    {
      path: '/dashboard',
      name: 'Framework',
      component: () => import("@/views/Framework.vue"),
      children: [
        {
          path: '/',
          redirect: "/dashboard/main/all"
        },
        {
          path: 'main/:category',
          name: '首页',
          meta: {
            needLogin: true,
            menuCode: "main"
          },
          component: () => import("@/views/main/Main.vue")
        },
        {
          path: 'myshare',
          name: '我的分享',
          meta: {
            needLogin: true,
            menuCode: "share"
          },
          component: () => import("@/views/share/Share.vue")
        },
        {
          path: 'recycle',
          name: '回收站',
          meta: {
            needLogin: true,
            menuCode: "recycle"
          },
          component: () => import("@/views/recycle/Recycle.vue")
        },
        {
          path: 'settings/sysSetting',
          name: '系统设置',
          meta: {
            needLogin: true,
            menuCode: "settings"
          },
          component: () => import("@/views/admin/SysSettings.vue")
        },
        {
          path: 'settings/userList',
          name: '用户管理',
          meta: {
            needLogin: true,
            menuCode: "settings"
          },
          component: () => import("@/views/admin/UserList.vue")
        },
        {
          path: 'settings/fileList',
          name: '用户文件',
          meta: {
            needLogin: true,
            menuCode: "settings"
          },
          component: () => import("@/views/admin/FileList.vue")
        },
        {
          path: 'payment',
          name: '会员升级',
          meta: {
            needLogin: true,
            menuCode: "payment"
          },
          component: () => import("@/views/payment/Payment.vue")
        },
        {
          path: 'payment/history',
          name: '支付记录',
          meta: {
            needLogin: true,
            menuCode: "payment"
          },
          component: () => import("@/views/payment/PaymentHistory.vue")
        }
      ]
    },
    {
      path: '/shareCheck/:shareId',
      name: '分享校验',
      component: () => import("@/views/webshare/ShareCheck.vue")
    },
    {
      path: '/share/:shareId',
      name: '分享',
      component: () => import("@/views/webshare/Share.vue")
    },
    {
      path: '/adminsystem',
      meta: { layout: 'empty' }, 
      name: '后台管理',
      component: () => import("@/views/adminsystem/AdminSystem.vue"),
      children: [
        {
          path: '',
          redirect: '/adminsystem/dashboard'
        },
        {
          path: 'dashboard',
          name: '仪表盘',
          component: () => import('@/views/adminsystem/Dashboard.vue')
        },
        {
          path: 'users',
          name: '用户管理',
          component: () => import('@/views/adminsystem/Users.vue')
        },
        {
          path: 'files',
          name: '文件管理',
          component: () => import('@/views/adminsystem/Files.vue')
        },
        {
          path: 'settings',
          name: '系统设置',
          component: () => import('@/views/adminsystem/Settings.vue')
        },
        {
          path: 'payment',
          name: '支付管理',
          component: () => import('@/views/adminsystem/Payment.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo")
  if (to.meta.needLogin != null && to.meta.needLogin && userInfo == null) {
    router.push("/login");
  }
  next();
})

export default router
